﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class PointExchange
    {
        public bool Add(List<Chain.Model.PointExchange> list)
        {
            int num = 0;
            ArrayList sQLStringList = new ArrayList();
            foreach (Chain.Model.PointExchange exchange in list)
            {
                num += exchange.ExchangeTotalPoint;
                StringBuilder builder = new StringBuilder();
                builder.Append("insert into PointExchange(");
                builder.Append("ExchangeMemID,ExchangeGiftID,ExchangeNumber,ExchangeTotalPoint,ExchangeShopID,ExchangeTime,ExchangeUserID)");
                builder.Append(" values (");
                builder.Append("@ExchangeMemID,@ExchangeGiftID,@ExchangeNumber,@ExchangeTotalPoint,@ExchangeShopID,@ExchangeTime,@ExchangeUserID)");
                builder.Append(";select @@IDENTITY");
                SqlParameter[] parameterArray = new SqlParameter[] { new SqlParameter("@ExchangeMemID", SqlDbType.Int, 4), new SqlParameter("@ExchangeGiftID", SqlDbType.Int, 4), new SqlParameter("@ExchangeNumber", SqlDbType.Int, 4), new SqlParameter("@ExchangeTotalPoint", SqlDbType.Int, 4), new SqlParameter("@ExchangeShopID", SqlDbType.Int, 4), new SqlParameter("@ExchangeTime", SqlDbType.DateTime), new SqlParameter("@ExchangeUserID", SqlDbType.Int, 4) };
                parameterArray[0].Value = exchange.ExchangeMemID;
                parameterArray[1].Value = exchange.ExchangeGiftID;
                parameterArray[2].Value = exchange.ExchangeNumber;
                parameterArray[3].Value = exchange.ExchangeTotalPoint;
                parameterArray[4].Value = exchange.ExchangeShopID;
                parameterArray[5].Value = exchange.ExchangeTime;
                parameterArray[6].Value = exchange.ExchangeUserID;
                sQLStringList.Add(new DictionaryEntry(builder.ToString(), parameterArray));
                string key = string.Format("Update PointGift set GiftStockNumber =GiftStockNumber -{0},GiftExchangeNumber = GiftExchangeNumber +{0} where GiftID={1}", exchange.ExchangeNumber, exchange.ExchangeGiftID);
                sQLStringList.Add(new DictionaryEntry(key, null));
            }
            return DbHelperSQL.ExecuteSqlTranPara(sQLStringList);
        }

        public int Add(Chain.Model.PointExchange model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into PointExchange(");
            builder.Append("ExchangeMemID,ExchangeGiftID,ExchangeNumber,ExchangeTotalPoint,ExchangeShopID,ExchangeTime,ExchangeUserID)");
            builder.Append(" values (");
            builder.Append("@ExchangeMemID,@ExchangeGiftID,@ExchangeNumber,@ExchangeTotalPoint,@ExchangeShopID,@ExchangeTime,@ExchangeUserID)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ExchangeMemID", SqlDbType.Int, 4), new SqlParameter("@ExchangeGiftID", SqlDbType.Int, 4), new SqlParameter("@ExchangeNumber", SqlDbType.Int, 4), new SqlParameter("@ExchangeTotalPoint", SqlDbType.Int, 4), new SqlParameter("@ExchangeShopID", SqlDbType.Int, 4), new SqlParameter("@ExchangeTime", SqlDbType.DateTime), new SqlParameter("@ExchangeUserID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.ExchangeMemID;
            cmdParms[1].Value = model.ExchangeGiftID;
            cmdParms[2].Value = model.ExchangeNumber;
            cmdParms[3].Value = model.ExchangeTotalPoint;
            cmdParms[4].Value = model.ExchangeShopID;
            cmdParms[5].Value = model.ExchangeTime;
            cmdParms[6].Value = model.ExchangeUserID;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int ExchangeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from PointExchange ");
            builder.Append(" where ExchangeID=@ExchangeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ExchangeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ExchangeID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string ExchangeIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from PointExchange ");
            builder.Append(" where ExchangeID in (" + ExchangeIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int ExchangeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from PointExchange");
            builder.Append(" where ExchangeID=@ExchangeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ExchangeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ExchangeID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ExchangeID,ExchangeMemID,ExchangeGiftID,ExchangeNumber,ExchangeTotalPoint,ExchangeShopID,ExchangeTime,ExchangeUserID ");
            builder.Append(" FROM PointExchange ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ExchangeID,ExchangeMemID,ExchangeGiftID,ExchangeNumber,ExchangeTotalPoint,ExchangeShopID,ExchangeTime,ExchangeUserID ");
            builder.Append(" FROM PointExchange ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.ExchangeID desc");
            }
            builder.Append(")AS Row, T.*  from PointExchange T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "GiftExchange,Mem,SysShop,SysUser";
            string[] column = new string[] { "ExchangeAccount,MemCard,MemName,ExchangeAllNumber,ExchangeAllPoint,ApplicationTime,ExchangeTime,ShopName,UserName,ExchangeType,ExchangeID,ExchangeStatus" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "GiftExchange.ExchangeID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ExchangeID", "PointExchange");
        }

        public Chain.Model.PointExchange GetModel(int ExchangeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 ExchangeID,ExchangeMemID,ExchangeGiftID,ExchangeNumber,ExchangeTotalPoint,ExchangeShopID,ExchangeTime,ExchangeUserID from PointExchange ");
            builder.Append(" where ExchangeID=@ExchangeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ExchangeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ExchangeID;
            Chain.Model.PointExchange exchange = new Chain.Model.PointExchange();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["ExchangeID"] != null) && (set.Tables[0].Rows[0]["ExchangeID"].ToString() != ""))
            {
                exchange.ExchangeID = int.Parse(set.Tables[0].Rows[0]["ExchangeID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ExchangeMemID"] != null) && (set.Tables[0].Rows[0]["ExchangeMemID"].ToString() != ""))
            {
                exchange.ExchangeMemID = int.Parse(set.Tables[0].Rows[0]["ExchangeMemID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ExchangeGiftID"] != null) && (set.Tables[0].Rows[0]["ExchangeGiftID"].ToString() != ""))
            {
                exchange.ExchangeGiftID = int.Parse(set.Tables[0].Rows[0]["ExchangeGiftID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ExchangeNumber"] != null) && (set.Tables[0].Rows[0]["ExchangeNumber"].ToString() != ""))
            {
                exchange.ExchangeNumber = int.Parse(set.Tables[0].Rows[0]["ExchangeNumber"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ExchangeTotalPoint"] != null) && (set.Tables[0].Rows[0]["ExchangeTotalPoint"].ToString() != ""))
            {
                exchange.ExchangeTotalPoint = int.Parse(set.Tables[0].Rows[0]["ExchangeTotalPoint"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ExchangeShopID"] != null) && (set.Tables[0].Rows[0]["ExchangeShopID"].ToString() != ""))
            {
                exchange.ExchangeShopID = int.Parse(set.Tables[0].Rows[0]["ExchangeShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ExchangeTime"] != null) && (set.Tables[0].Rows[0]["ExchangeTime"].ToString() != ""))
            {
                exchange.ExchangeTime = DateTime.Parse(set.Tables[0].Rows[0]["ExchangeTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ExchangeUserID"] != null) && (set.Tables[0].Rows[0]["ExchangeUserID"].ToString() != ""))
            {
                exchange.ExchangeUserID = int.Parse(set.Tables[0].Rows[0]["ExchangeUserID"].ToString());
            }
            return exchange;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM PointExchange ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.PointExchange model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update PointExchange set ");
            builder.Append("ExchangeMemID=@ExchangeMemID,");
            builder.Append("ExchangeGiftID=@ExchangeGiftID,");
            builder.Append("ExchangeNumber=@ExchangeNumber,");
            builder.Append("ExchangeTotalPoint=@ExchangeTotalPoint,");
            builder.Append("ExchangeShopID=@ExchangeShopID,");
            builder.Append("ExchangeTime=@ExchangeTime,");
            builder.Append("ExchangeUserID=@ExchangeUserID");
            builder.Append(" where ExchangeID=@ExchangeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ExchangeMemID", SqlDbType.Int, 4), new SqlParameter("@ExchangeGiftID", SqlDbType.Int, 4), new SqlParameter("@ExchangeNumber", SqlDbType.Int, 4), new SqlParameter("@ExchangeTotalPoint", SqlDbType.Int, 4), new SqlParameter("@ExchangeShopID", SqlDbType.Int, 4), new SqlParameter("@ExchangeTime", SqlDbType.DateTime), new SqlParameter("@ExchangeUserID", SqlDbType.Int, 4), new SqlParameter("@ExchangeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.ExchangeMemID;
            cmdParms[1].Value = model.ExchangeGiftID;
            cmdParms[2].Value = model.ExchangeNumber;
            cmdParms[3].Value = model.ExchangeTotalPoint;
            cmdParms[4].Value = model.ExchangeShopID;
            cmdParms[5].Value = model.ExchangeTime;
            cmdParms[6].Value = model.ExchangeUserID;
            cmdParms[7].Value = model.ExchangeID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

